package com.red.battleworm.view {
	import flash.events.*;	
	import flash.text.*;	
	import flash.display.*;	
	
	import org.puremvc.as3.interfaces.*;	
	
	import com.red.battleworm.*;	
	
	import org.puremvc.as3.patterns.mediator.*;
	
	/**
	 * @author Administrator
	 */
	public class LoginMediator extends Mediator {
		
		public function LoginMediator(viewComponent : Object = null) {
			super("LoginMediator", viewComponent);
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ AppMain.SHOWLOGIN];
		}
        
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) {
            	case AppMain.SHOWLOGIN:
            		init();
            		break;
            }
        }
        
        protected function get stage():Sprite{
            return viewComponent as Sprite;
        }
        
		private var _page:Sprite;
		private var _username:TextField;
		private var _password:TextField;
		private var _submit:SimpleButton;
		
        public function init() : void {
        	_page = new LoginPage();
			_username = TextField(_page.username);
			_password = TextField(_page.password);
			_submit = SimpleButton(_page.submit);
			_submit.addEventListener(MouseEvent.MOUSE_UP, onSubmit);
			stage.addChild(_page);
		}
		
		public function destroy() : void {
			_submit.removeEventListener(MouseEvent.MOUSE_UP, onSubmit);
			stage.removeChild(_page);
		}
		
		private function onSubmit(e:MouseEvent) : void {
			sendNotification(AppMain.LOGIN, {"username":_username.text, "password":_password.text});
		}
	}
}
